package cn.snoworld.controller;

import cn.snoworld.entity.Picture;
import cn.snoworld.service.FileUploadService;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author zxj
 * 上传图片控制层
 */
@Controller
public class FileUploadController {
    @Value("${storage.minio.bucket-name}")
    private String bucketName;

    @Autowired
    private MinioClient minioClient;
    @Autowired
    private FileUploadService uploadService;

    @ResponseBody
    @GetMapping("/test")
    public String a() throws Exception {
        System.out.println(bucketName);
        System.out.println(minioClient.bucketExists(bucketName));
        System.out.println(minioClient);
        System.out.println(1 / 0);
        return "";
    }

    /**
     * 测试数据库插入接口
     *
     * @param picture
     * @return
     */
    @ResponseBody
    @PostMapping("/testConn")
    public String fileUpload(@RequestBody Picture picture) {
        System.out.println(picture);
        uploadService.save(picture);
        return "接收成功";
    }

    /**
     * 文件上传接口
     *
     * @param file 文件
     * @return Boolean
     */
    @PostMapping("/upload")
    @ResponseBody
    public Boolean upload(MultipartFile file) throws IOException {
        return uploadService.upload(file);
    }


}
